package com.example.zqwl;

import com.alibaba.fastjson.JSONObject;

public class RetDataParse {
	//1.解析开机上报信息
	public static dev_conn_info parse_StartUpload(String ip,String msg) {
		//示例:{"addr":"1","cmd":"upload","IMEI":861714058833058,"time":"2022-09-24 16:39:04","gps":[0.0,0.0]}
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("cmd");
		if(key1==null)
			return null;
		if(!"upload".equalsIgnoreCase(key1))
			return null;
		dev_conn_info info=new dev_conn_info();
		String imei=null;
		imei=jsonObject.getString("IMEI");
		if(imei==null)
			imei=jsonObject.getString("imei");
		info.setImei(imei);
		info.setIp(ip);
		info.setLogintime(jsonObject.getString("time"));
		info.setGps(jsonObject.getString("gps"));

		return info;
	}
	//2.解析设备基础信息，对应命令1
	public static dev_basic_info parse_BasicInfo(String msg) {
		//示例：{"rmtcmd":"retget","dev":{"info":{"rssi":20,"run":101,"gps":"0 0.000000 0.000000","ver":"V23063","model":"2100YD-CAT1","imei":"861714058833058","mac":"","iccid":"898607D8062230125738","type":10,"x":0,"y":0,"adc":0,"freem":773,"status":{"sa":1,"sb":1,"http":0,"mqtt":0,"mqtcp":0,"mqsub":0}}},"imei":"861714058833058"}
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("rmtcmd");
		if(key1==null||!key1.equalsIgnoreCase("retget"))
			return null;
		String key2=null;
		key2=jsonObject.getString("dev");
		if(key2==null)
			return null;
		JSONObject dev=jsonObject.getJSONObject("dev");
		String key3=null;
		key3=dev.getString("info");
		if(key3==null)
			return null;
		dev_basic_info info=new dev_basic_info();
		info.setModel(dev.getJSONObject("info").getString("model"));
		info.setVer(dev.getJSONObject("info").getString("ver"));
		info.setIccid(dev.getJSONObject("info").getString("iccid"));
        info.setImei(dev.getJSONObject("info").getString("imei"));
        info.setMac(dev.getJSONObject("info").getString("mac"));
        info.setRun(dev.getJSONObject("info").getInteger("run"));
        info.setFreem(dev.getJSONObject("info").getInteger("freem"));
        info.setRssi(dev.getJSONObject("info").getInteger("rssi"));
        info.setGps(dev.getJSONObject("info").getString("gps"));
        info.setType(dev.getJSONObject("info").getInteger("type"));
        info.setX(dev.getJSONObject("info").getInteger("x"));
        info.setY(dev.getJSONObject("info").getInteger("y"));
        info.setAdc(dev.getJSONObject("info").getInteger("adc"));
        String status=null;
        status=dev.getJSONObject("info").getString("status");
        //info.setStatus(status);
        if(status!=null) {
        	info.setSa(dev.getJSONObject("info").getJSONObject("status").getInteger("sa"));
            info.setSb(dev.getJSONObject("info").getJSONObject("status").getInteger("sb"));
            info.setHttp(dev.getJSONObject("info").getJSONObject("status").getInteger("http"));
            info.setMqtt(dev.getJSONObject("info").getJSONObject("status").getInteger("mqtt"));
        }

		return info;
	}
	//3.解析sa、mqtt服务器连接以及串口信息，对应命令2
	public static dev_server_info parse_ServerInfo(String msg) {
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("rmtcmd");
		if(key1==null||!key1.equalsIgnoreCase("retget"))
			return null;
		String key2=null;
		key2=jsonObject.getString("dev");
		if(key2==null)
			return null;
		JSONObject dev=jsonObject.getJSONObject("dev");
		String key3=null;
		key3=dev.getString("wm");
		String key4;
		key4=dev.getString("sa");
		String key5=null;
		key5=dev.getString("mqtt");
		String key6=null;
		key6=dev.getString("uart");
		if(key4==null||key5==null)
			return null;

		dev_server_info info=new dev_server_info();

		if(key3!=null) {
			info.setMode(dev.getJSONObject("wm").getInteger("mode"));
		}

		if(key4!=null) {
			info.setSa_ip(dev.getJSONObject("sa").getString("ip"));
			info.setSa_port(dev.getJSONObject("sa").getInteger("port"));
			info.setUdp(dev.getJSONObject("sa").getInteger("udp"));
		}

		if(key5!=null) {
			info.setM_ip(dev.getJSONObject("mqtt").getString("ip"));
			info.setM_port(dev.getJSONObject("mqtt").getInteger("port"));
			info.setUn(dev.getJSONObject("mqtt").getString("un"));
			info.setUp(dev.getJSONObject("mqtt").getString("up"));
			info.setId(dev.getJSONObject("mqtt").getString("id"));
			info.setPub1(dev.getJSONObject("mqtt").getString("pub1"));
			info.setSub(dev.getJSONObject("mqtt").getString("sub"));
			info.setKa(dev.getJSONObject("mqtt").getInteger("ka"));
		}

		if(key6!=null) {
			info.setRate(dev.getJSONObject("uart").getInteger("rate"));
			info.setCheck(dev.getJSONObject("uart").getInteger("check"));
			info.setPack(dev.getJSONObject("uart").getInteger("pack"));
			//info.setDataw(dev.getJSONObject("uart").getString(""));
		}

		info.setImei(jsonObject.getString("imei"));

		return info;
	}
	//4.解析modbus轮询配置基本信息，对应命令4
	public static dev_modbus_basic_info parse_ModbusBasicInfo(String msg) {
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("rmtcmd");
		if(key1==null||!key1.equalsIgnoreCase("retget"))
			return null;
		String key2=null;
		key2=jsonObject.getString("dev");
		if(key2==null)
			return null;
		JSONObject dev=jsonObject.getJSONObject("dev");
		String key3=null;
		key3=dev.getString("poll");
		if(key3==null)
			return null;
		dev_modbus_basic_info info=new dev_modbus_basic_info();
		info.setCmdcnt(dev.getJSONObject("poll").getInteger("cmdcnt"));
		info.setDelay(dev.getJSONObject("poll").getInteger("delay"));
		info.setPrefix(dev.getJSONObject("poll").getString("prefix"));
		info.setTimeout(dev.getJSONObject("poll").getInteger("timeout"));
		info.setTimestamp(dev.getJSONObject("poll").getInteger("timestamp"));
		info.setUpdate(dev.getJSONObject("poll").getInteger("update"));
		info.setUploadchange(dev.getJSONObject("poll").getInteger("uploadchange"));
		info.setUploadtime(dev.getJSONObject("poll").getInteger("uploadtime"));
		info.setUploadtype(dev.getJSONObject("poll").getInteger("uploadtype"));

		info.setImei(jsonObject.getString("imei"));

		return info;
	}
	//5.解析modbus轮询配置寄存器命令，对应命令6
	public static dev_modbus_cmd_info parse_ModbusCmd(String msg) {
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("rmtcmd");
		if(key1==null||!key1.equalsIgnoreCase("retget"))
			return null;
		String key2=null;
		key2=jsonObject.getString("dev");
		if(key2==null)
			return null;
		JSONObject dev=jsonObject.getJSONObject("dev");
		String key3=null;
		key3=dev.getString("pollcmd");
		if(key3==null)
			return null;
		dev_modbus_cmd_info info=new dev_modbus_cmd_info();
		JSONObject cmd=dev.getJSONObject("pollcmd").getJSONObject("cmd");
		info.set_float(cmd.getInteger("float"));
		info.setAddr(cmd.getInteger("addr"));
		info.setDtype(cmd.getInteger("dtype"));
		info.setEm(cmd.getInteger("em"));
		info.setFun(cmd.getInteger("fun"));
		info.setIdx(cmd.getInteger("idx"));
		info.setKey(cmd.getString("key"));
		info.setRaddr(cmd.getInteger("raddr"));
		info.setRsize(cmd.getInteger("rsize"));
		info.setTv1(cmd.getInteger("tv1"));
		info.setTv2(cmd.getInteger("tv2"));

		info.setImei(jsonObject.getString("imei"));

		return info;
	}
	//6.解析设置（保存）基础参数的应答
	public static String parse_ReturnSet(String msg) {
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("rmtcmd");
		if(key1==null||!key1.equalsIgnoreCase("retset"))
			return null;
		String key2=null;
		key2=jsonObject.getString("status");
		if(key2.equalsIgnoreCase("1"))
			return "set_success.";
		else
			return "set_fail.";
	}
	//7.解析重启指令应答
	public static String parse_ReturnReset(String msg) {
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String key1=null;
		key1=jsonObject.getString("rmtcmd");
		if(key1==null||!key1.equalsIgnoreCase("retreset"))
			return null;
		String key2=null;
		key2=jsonObject.getString("status");
		if(key2.equalsIgnoreCase("1"))
			return "reset_success.";
		else
			return "reset_fail.";
	}
}

